﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import fl.controls.Label;
	import flash.external.ExternalInterface;
	import flash.net.NetStream;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	
	public class Main extends MovieClip {
		
		private var carm:Camera = Camera.getCamera();//得到系统的默认摄像头
		private var localVideo:Video=new Video(); //本地视频
		private var remoteVideo:Video=new Video();	//远程视频
		private var isLocalMax:Boolean = true;	//本地视频最大化
		private static const serverUrl:String ="rtmp://192.168.1.115/HelloRed5/"; //server url
		private var conn:NetConnection;
		private var ns:NetStream;
			
		public function Main() {
			try{
				//连接服务器
				connectServer();
				
				//视频显示
				initMainContainer();
						
				//本地、远程视频切换显示
				this.addEventListener(MouseEvent.CLICK, changeCameraHandler);
				this.buttonMode = true;
				
				if (ExternalInterface.available) {
					ExternalInterface.addCallback("closeCamera", closeCamera);
				}
				
				//连接服务器
				conn.connect(serverUrl);
			}catch(error:Error){
				
			}
		}
		
		/**
			连接服务器
		**/
		private function connectServer():void{
			conn = new NetConnection();
			conn.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
		}
		
		private function netStatus(event:NetStatusEvent):void{
			if(event.info.code == "NetConnection.Connect.Success"){
				ns = new NetStream(conn);
				ns.client = new StreamClient();
				remoteVideo.attachNetStream(ns);
				ns.play("1");
			}
		}
		
		/**
			关闭摄像头
		**/
		public function closeCamera():void{
			if(carm){
				carm = null;
				localVideo.attachCamera(null);
				remoteVideo.attachCamera(null);
			}
		}
		
		/**
			切换显示
		**/
		protected function changeCameraHandler(event:MouseEvent):void{
			if(isLocalMax){
				localVideo.width=98;
				localVideo.height=98;
						
				remoteVideo.width=398;
				remoteVideo.height=358;
				mainContainer.addChild(remoteVideo);
				smallContainer.addChild(localVideo);
			}else{
				localVideo.width=398;
				localVideo.height=358;
						
				remoteVideo.width=98;
				remoteVideo.height=98;
				mainContainer.addChild(localVideo);
				smallContainer.addChild(remoteVideo);
			}
			isLocalMax = !isLocalMax;
		}
		
		private function initMainContainer():void{
			smallContainer.graphics.beginFill(0xFFFFFF, 1);
			smallContainer.graphics.drawRect(1, 1, 98, 98);
			mainContainer.graphics.beginFill(0xFFFFFF, 1);
			mainContainer.graphics.drawRect(1, 1, 398, 398);
				
			localVideo.width=398;
			localVideo.height=358;
			
			remoteVideo.width=98;
			remoteVideo.height=98;
			if(carm){
				carm.setQuality(0,1);
				carm.setMode(1920, 1080, 25);
				// 把视频放进去
				localVideo.attachCamera(carm);
				//remoteVideo.attachCamera(carm);
				mainContainer.addChild(localVideo);
				smallContainer.addChild(remoteVideo);
			}else{
				mainContainer.addChild(localVideo);
			}
			
		}
		
		/**
			检查是否安装好摄像头
		**/
		public function checkCamera():Boolean{
			if(carm){
				return true;
			}else{
				return false;
			}
		}
	}
	
}
